<?php

namespace app\services\setting\admin;

use app\dao\setting\admin\SystemAdminDao;
use app\exceptions\AdminException;
use app\exceptions\AuthException;
use app\model\exam\ExamPaper;
use app\model\exam\ExamQuestion;
use app\model\exam\ExamSend;
use app\model\exam\ExamStatus;
use app\model\office\OfficeUser;
use app\services\BaseService;
use app\services\exam\ExamQuestService;
use app\utils\Token;

class SystemAdminService extends BaseService
{
    public function __construct(SystemAdminDao $dao)
    {
        $this->dao = $dao;
    }

    public function index(array $where)
    {
        return $this->dao->index($where);
    }

    public function login(array $where)
    {
        $data = $this->dao->login($where);
        if (!$data) {
            throw new AdminException('管理员不存在');
        }
        if (!$data->status) {
            throw new AdminException('您已被禁止登录');
        }
        if (!($where['password'] == $data['password'])) {
            throw new AdminException('账号或密码错误，请重新输入');
        }
        return ['accessToken' =>Token::getToken('userId', $data['id']),'refreshJwt' => Token::getRefreshJwt('userId',$data['id'],3600 * 24 * 30)];//一个月
    }

    public function info(array $where)
    {
        $Jwt = Token::verifyToken($where['accessToken']);
        if (!$Jwt) throw  new AdminException('accessToken不合法！',402);
        $userId = Token::getUserId($where['accessToken'], 'userId');
        return $this->dao->info($userId);
    }

    public function create(array $where)
    {
        return $this->dao->create($where);
    }

    public function delete(array $where)
    {
        return $this->dao->deleteDao($where);

    }

    public function update(array $where)
    {
        return $this->dao->updateDa0($where);
    }

    public function searchAccount(array $where)
    {
        return $this ->dao -> searchAccount($where);
    }

    public function indexData(array $where)
    {
       $data['stu'] =  (new OfficeUser()) -> where('website_id',$where['website_id'])  -> count(); //统计所有用户
       $data['topic'] = (new ExamQuestion()) -> where('website_id',$where['website_id'])  ->count();//统计所有题目
       $data['paper'] = (new ExamPaper()) -> where('website_id',$where['website_id'])  -> count();// 统计所有试卷
       $data['exam'] = (new ExamStatus()) -> where('website_id',$where['website_id']) -> count();//统计所有考试次数
       return $data;

    }

}